package com.czj.controller;


import com.czj.common.api.CommonResult;
import com.czj.common.exception.ApiException;
import com.czj.common.service.RedisService;
import com.czj.dto.AParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;


@Api(tags = "TestController",description = "测试相关接口")
@RestController
public class TestController {


    // produces = "application/json" 说明返回的是json
    @ApiOperation(value = "专门测试swagger")// 说明当前接口的作用
    @RequestMapping(value = "/test1",method = RequestMethod.GET)
    public String test1(String  message){
        // 此时不是标注的json  ,如果配置 produces = "application/json" ，swagger 就不显示，但是浏览器测试没有问题
        return "hello wold ----message:"+message;
    }


    /**
     * 使用同一数据格式 返回
     * @param data
     * @return
     */
    @ApiOperation(value = "测试返回同一数据格式",produces = "application/json")
    @RequestMapping(value = "/test2",method = RequestMethod.GET)
    public CommonResult<String> test2(String data){


        // 返回成功    使用CommonResult静态方法，快速的返回数据---------》避免newCommonResult 对象，会让代码更加的干净
        return CommonResult.success(data);

        // 返回失败
//        return  CommonResult.failed("服务器异常");
    }



    @Autowired
    private RedisService redisService;


    @ApiOperation(value = "测试redis 封装",produces = "application/json")
    @RequestMapping(value = "/redisTest",method = RequestMethod.GET)
    public CommonResult<String>  redisTest(String key,String value){

        redisService.set(key,value);

        return CommonResult.success("添加数据成功");
    }


    @ApiOperation("测试异常")
    @RequestMapping("/testException")
    public String testException(String name){


        // 抛一个异常
//        int i = 1/0;

        if(name.equals("root")){
            throw  new ApiException("程序异常");

            // 等价于    throw  new ApiException("程序异常");
//            Asserts.fail("程序异常");
        }


        return "xxx";
    }

    @ApiOperation(value = "自定义校验")
    @RequestMapping(value = "/validateTest", method = RequestMethod.POST)
    // @Validated 激活我们自定义的参数校验注解
    public CommonResult validateTest(@Validated @RequestBody AParam aParam) {
        CommonResult commonResult;


        commonResult = CommonResult.failed();

        return commonResult;
    }


}
